{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python常见问题\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[toc]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 局部变量和全局变量的问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 函数内部只作引用的 Python 变量隐式视为全局变量。\n",
    "* 如果在函数内部任何位置为变量赋值，则除非明确声明为全局变量，否则均将其视为局部变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    },
    {
     "ename": "SyntaxError",
     "evalue": "no binding for nonlocal 'x' found (<ipython-input-2-80d9d2aba8ae>, line 10)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-80d9d2aba8ae>\"\u001b[1;36m, line \u001b[1;32m10\u001b[0m\n\u001b[1;33m    nonlocal x\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m no binding for nonlocal 'x' found\n"
     ]
    }
   ],
   "source": [
    "# 正常运行\n",
    "x = 10\n",
    "def bar():\n",
    "    print(x)\n",
    "bar()\n",
    "\n",
    "# 声明全局\n",
    "def bar_1():\n",
    "    global x\n",
    "    # nonlocal x 只用于闭包,外层变量\n",
    "    # nonlocal x\n",
    "    x+=1\n",
    "    print(x)\n",
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 错误运行,局部变量\n",
    "y = 10 \n",
    "def bad():\n",
    "    for i in range(5):\n",
    "        y+=1\n",
    "    return y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lambda 返回相同的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "ans = []\n",
    "for i in range(5):\n",
    "    ans.append(lambda: i**2)\n",
    "\n",
    "print(ans[0]())\n",
    "print(ans[2]())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# i 是定义在lambda之外的全局变量,而不是lambda内的局部变量\n",
    "# i 是在调用lambda时才使用,因此是循环结束后的4\n",
    "# i 改成8,则lambda照样变化\n",
    "\n",
    "i=8\n",
    "ans[2]()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16]\n"
     ]
    }
   ],
   "source": [
    "# 解决办法是用一个局部变量承接i的值\n",
    "ans = []\n",
    "for i in range(5):\n",
    "    ans.append(lambda n=i: n**2)\n",
    "\n",
    "# ans[0]()\n",
    "print([i() for i in ans])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对象之间共享默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bad(mydic={}): # 有数据重复的风险, 最好使用tuple set这种不可变的数据结构\n",
    "    mydic[2] = 1\n",
    "    return mydic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def right(mydic = None):\n",
    "    if not mydic:\n",
    "        mydic = {} # 新建一个空的mydic\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 缓存技巧:\n",
    "def memorise(arg1, arg2, *, _cache={}):\n",
    "    if (arg1,arg2) in _cache:\n",
    "        return _cache[(arg1,arg2)]\n",
    "\n",
    "    result = None\n",
    "    pass\n",
    "    _cache[(arg1,arg2)] = result\n",
    "    return result\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 深拷贝浅拷贝修改列表y 也会修改列表x copy deepcopy\n",
    "\n",
    "改变的原因:\n",
    "1. 变量只是名称,x,y指向同一个对象\n",
    "2. x是可变的对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import copy\n",
    "\n",
    "x = [[1]]\n",
    "\n",
    "# 改变y 是否改变x\n",
    "# y = x #赋值\n",
    "# y = copy.deepcopy(x) # 深拷贝,x不变\n",
    "y = copy.copy(x) # 浅拷贝\n",
    "y.append(10)\n",
    "x\n",
    "\n",
    "z = 2\n",
    "y = z\n",
    "y+=1\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python中创建高阶函数\n",
    "* 嵌套\n",
    "* 可调用对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def linear(a, b):\n",
    "    def result(x):\n",
    "        return a*x+b\n",
    "    return result\n",
    "\n",
    "class linear_class():\n",
    "    def __init__(self, a, b):\n",
    "        self.a ,self.b = a,b\n",
    "    def __call__(self,x):\n",
    "        return self.a*x+self.b\n",
    "    \n",
    "linear(2,2)(2) # 函数\n",
    "Linear = linear_class(2,2) # 实例化类\n",
    "Linear(2) # 可调用对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## / 和 * 在函数参数中的意义\n",
    "如果你希望调用者使用函数时一定不能使用关键字参数传参，那么只需要把这些参数放在/前即可；如果你希望调用者使用函数时一定要使用某些参数，且必须为关键字参数传参，那么只需要把这些参数放在*后面即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "mark() got some positional-only arguments passed as keyword arguments: 'a'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-51ccd329b2b9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmark\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# a,b 不能用关键词赋值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mmark\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 出错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmark2\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: mark() got some positional-only arguments passed as keyword arguments: 'a'"
     ]
    }
   ],
   "source": [
    "def mark(a,b,/,c): # a,b 不能用关键词赋值\n",
    "    return a+b+c\n",
    "mark(2,3,a = 1) # 出错\n",
    "\n",
    "def mark2(a,b,*,c):\n",
    "    return a+b+c\n",
    "\n",
    "mark2(a=1,b=2,c = 3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10e9 和 10**9\n",
    "\n",
    "10e9 为浮点数\n",
    "10**9 为整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3527e6919fb3c5de6d1946832ad613724bb5bc6752cd9e3fcbde93ba38fe29f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.11 64-bit (conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
